package org.ling.xuan.rule;

import cn.hutool.core.util.StrUtil;
import org.ling.xuan.config.SysConstant;
import org.ling.xuan.config.SystemProperties;
import org.ling.xuan.context.AddPipeLineContext;
import org.ling.xuan.context.CheckChannelContext;
import org.ling.xuan.context.CheckPipelineContext;
import org.ling.xuan.entity.HundSonInfo;
import org.ling.xuan.entity.mo.LocalAdd;
import org.ling.xuan.util.LxStrUtil;

public class HundSunVersionCheckRule extends CheckRule<CheckChannelContext, CheckPipelineContext> {
    @Override
    public void check(CheckPipelineContext pipeContext, CheckChannelContext channelContext) {
        HundSonInfo hundSonInfo = pipeContext.getHundSonInfo();
        //头部区域检查
        if (StrUtil.isBlank(hundSonInfo.getAnnotationText())) {
            pipeContext.error("文件缺乏头部注释");
        } else {
            // 检查文件名称
            if (StrUtil.isBlank(hundSonInfo.getAnnotationFileName()) || !hundSonInfo.getAnnotationFileName().contains(hundSonInfo.getFileName())) {
                pipeContext.error("头部注释区域文件名称不一致");
            }
            if (StrUtil.isBlank(hundSonInfo.getLastAnnotationText())) {
                pipeContext.error("头部注释区域最后修改信息未填写");
            } else {
                if(SystemProperties.isTrueDefaultTrue(SysConstant.checkJavaPAnnotation)){
                    //检查Java非个性化类的注释不能包含银行,商行字样
                    if(!hundSonInfo.getFilePath().contains("bank")&&(hundSonInfo.getLastAnnotationText().contains("银行")||hundSonInfo.getLastAnnotationText().contains("商行"))){
                        pipeContext.error("非个性化代码注释不能带有个性化字样");
                    }
                }
                //版本检查
                String curVersion = StrUtil.isBlank(hundSonInfo.getMethodVersion()) ? hundSonInfo.getHundSonVersion() : hundSonInfo.getMethodVersion();
                if (StrUtil.isBlank(curVersion)) {
                    pipeContext.error("缺乏版本声明,请添加getVersion方法或者声明HUNDSUN静态成员变量");
                } else {
                    if (!curVersion.equals(hundSonInfo.getLastAnnotationVersion())) {
                        pipeContext.error("头部注释最后修改信息版本和当前文件getVersion或HUNDSUN静态成员变量声明不一致,请检查");
                    }
                }
                //检查作者
                if (!hundSonInfo.getLastAnnotationText().contains(hundSonInfo.getAuthSetting())) {
                    pipeContext.error("头部注释最后一行作者Str not contains(开发者自己),请检查或者修改lx-tool插件auth设定");
                }
                //检查修改单号
                if (StrUtil.isBlank(hundSonInfo.getLastAnnotationModifyId())) {
                    pipeContext.error("头部注释最后修改信息缺乏修改单号,请填写");
                } else {
                    if (pipeContext instanceof AddPipeLineContext) {
                        LocalAdd localAdd = pipeContext.getLocalAdd();
                        if (localAdd == null || StrUtil.isBlank(localAdd.getModifyId())) {
                            pipeContext.warn("修改单号未填,无法检测文件修改单号信息");
                        } else {
                            String[] ss = localAdd.getModifyId().split(";");
                            if (!LxStrUtil.containsLeastOne(hundSonInfo.getLastAnnotationModifyId(),ss)) {
                                pipeContext.error("修改的文件头部最后一行注释的修改单号和当前修改单号不符");
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public boolean match(CheckPipelineContext pipeContext, CheckChannelContext channelContext) {
        return pipeContext.getFilePath().endsWith(".java") && SystemProperties.isTrueDefaultTrue(SysConstant.checkHundSunJavaFile);
    }
}
